package org.raphets.todayinhistory.http;

import org.raphets.todayinhistory.bean.GrilBean;
import org.raphets.todayinhistory.bean.Histroy;
import org.raphets.todayinhistory.bean.Picture;
import org.raphets.todayinhistory.bean.SimpleHistory;
import org.raphets.todayinhistory.common.Constants;
import org.raphets.todayinhistory.utils.MyLog;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Observable;

/**
 * Created by RaphetS on 2016/10/15.
 */

public class RetrofitHelper {

    private static RetrofitHelper mRetrofitHelper;
    private  APIService mApiService;
    private APIService mGirlApiService;
    private RequestServes requestSerives;
    private final String TAG = "RetrofitHelper";

    public RetrofitHelper() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://v.juhe.cn/todayOnhistory/")
                //增加返回值为String的支持
                .addConverterFactory(ScalarsConverterFactory.create())
                //增加返回值为Gson的支持(以实体类返回)
                .addConverterFactory(GsonConverterFactory.create())
                //增加返回值为Oservable<T>的支持
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mApiService = retrofit.create(APIService.class);
        requestSerives = retrofit.create(RequestServes.class);
        Retrofit grilRetofit= new Retrofit.Builder()
                .baseUrl("http://gank.io/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mGirlApiService =grilRetofit.create(APIService.class);

    }


    public static RetrofitHelper getInstance() {
        if (mRetrofitHelper == null) {
            synchronized (RetrofitHelper.class) {
                if (mRetrofitHelper == null) {
                    mRetrofitHelper = new RetrofitHelper();
                }
            }
        }
        return mRetrofitHelper;
    }


    /**
     * 获取历史列表
     */
    public  Observable<HttpResponse<SimpleHistory>> getHistoryList(int month, int date) {
        if (mApiService==null){
            new RetrofitHelper();
        }
        return mApiService.getHistoryList(Constants.APP_KEY, month+"/"+date);
    }


    /**
     * 获取历史列表
     */
    public void getHistoryString(int month, int date) {
        if (requestSerives == null){
            new RetrofitHelper();
        }
        Call<String> call = requestSerives.getString(Constants.APP_KEY, month + "/" + date);//传入我们请求的键值对的值
        //利用得到的Call对象，然后我们就发出网络请求了：
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                MyLog.e(TAG,"return:" + response.body().toString());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                MyLog.e(TAG, "失败");
            }
        });
    }

    /**
     * 获取历史详情
     * @param eid
     */
    public  Observable<HttpResponse<Histroy<Picture>>> getHistoryDetail(String eid) {
        if (mApiService==null){
            new RetrofitHelper();
        }
        return mApiService.getHitoryDetail(Constants.APP_KEY,eid);
    }

    /**
     * 获取妹子列表
     */
    public Observable<GrilHttppResponse<List<GrilBean>>> getGrilList(int page,int num){
        if (mGirlApiService ==null){
            new RetrofitHelper();
        }
        return mGirlApiService.getGirlList(num,page);
    }

}
